<template>
    <div class="content" @keyup.enter.native="showSelectRole ? selectRole() : login()">
        <div class="container">
            <form class="form" v-loading="isLoading" element-loading-text="正在登录~">
                <div class="tit">{{ showSelectRole ? '选择登录角色' : '登录' }}</div>
                <div class="login" v-if="!showSelectRole">
                    <input type="text" placeholder="账号" v-model="form.userNo" />
                    <span class="prompt">{{ prompt.userNo }}</span>
                    <input type="password" placeholder="密码" autocomplete="off" v-model="form.password" />
                    <span class="prompt">{{ prompt.password }}</span>
                    <div class="code">
                        <input type="text" placeholder="验证码" v-model="form.captcha" />
                        <img @click="getCaptchaImage()" alt="图片无法显示" :src="captchaImage">
                    </div>
                    <span class="prompt">{{ prompt.captcha }}</span>
                </div>
                <div class="selectRole" v-else>
                    <el-select clearable v-model="form.roleId" placeholder="请选择登录角色" size="large">
                        <el-option v-for="item in roleData" :key="item.roleId" :label="item.roleName"
                            :value="item.roleId" />
                    </el-select>
                    <span class="prompt">{{ prompt.roleId }}</span>
                </div>
                <button :disabled="isLoading" type="button" @click="showSelectRole ? selectRole() : login()">
                    {{ showSelectRole ? '确认' : '登录' }}
                </button>
                <span v-if="!showSelectRole">基于热门大模型的AI助手！<a href="#/ai">去体验</a></span>
                <span v-else><a href="#/login" @click="backLogin()">返回登录</a></span>
            </form>
        </div>
        <div class="square">
            <ul class="list">
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
            </ul>
        </div>
        <div class="circle">
            <ul class="list">
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
                <li class="item"></li>
            </ul>
        </div>
    </div>
</template>
<script setup>
import api from '../../api'
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const isLoading = ref(false)
const captchaImage = ref()
const prompt = ref({})
const form = ref({})
const roleData = ref([])
const showSelectRole = ref(false)
//获取验证码
const getCaptchaImage = async () => {
    const res = await api.get('/author/captchaImage')
    if (res.data.code === 200) {
        captchaImage.value = res.data.data.code
        form.value.key = res.data.data.key
    } else {
        ElMessage.error(res.data.msg)
    }
}
//登录
const login = async () => {
    //校验参数
    const flag = verifyForm()
    if (flag) {
        return
    }
    isLoading.value = true
    //加密
    const userNo = btoa(form.value.userNo)
    const password = btoa(form.value.password)
    const param = {
        "userNo": userNo,
        "password": password,
        "captcha": form.value.captcha,
        "key": form.value.key,
    }
    const res = await api.post('/author/login', param)
    if (res.data.code === 200) {
        const userInfo = res.data.data
        if (userInfo.token) {
            localStorage.setItem("access-admin", JSON.stringify(userInfo))
            router.push("/home")
        } else {
            roleData.value = userInfo.roles
            showSelectRole.value = true
        }
    } else {
        getCaptchaImage()
        ElMessage.error(res.data.msg)
    }
    isLoading.value = false
}
//校验登录参数
const verifyForm = () => {
    let flag = false
    prompt.value = {}
    var chineseRegex = /[\u4E00-\u9FFF]/g;
    if (!form.value.userNo) {
        prompt.value.userNo = '登录账号不能为空！'
        flag = true
    } else if (chineseRegex.test(form.value.userNo)) {
        prompt.value.userNo = '登录账号不能有汉字！'
        flag = true
    }
    if (!form.value.password) {
        prompt.value.password = '登录密码不能为空！'
        flag = true
    }
    if (!form.value.captcha) {
        prompt.value.captcha = '验证码不能为空！'
        flag = true
    }
    return flag
}
//选择角色
const selectRole = async () => {
    if (!form.value.roleId) {
        prompt.value.roleId = '请选择角色'
        return
    }
    isLoading.value = true
    //加密
    const userNo = btoa(form.value.userNo)
    const password = btoa(form.value.password)
    const param = {
        "userNo": userNo,
        "password": password,
        "roleId": form.value.roleId,
    }
    const res = await api.post('/author/token', param)
    if (res.data.code === 200) {
        const userInfo = res.data.data
        localStorage.setItem("access-admin", JSON.stringify(userInfo))
        router.push("/home")
    } else {
        ElMessage.error(res.data.msg)
    }
    isLoading.value = false
}
//返回登录
const backLogin = () => {
    getCaptchaImage();
    form.value = {}
    showSelectRole.value = false;
}

//设置全局主题颜色
const getThemeColor = () => {
    const themeColor = localStorage.getItem('theme-color')
    const subThemeColor = localStorage.getItem('sub-theme-color')
    if (themeColor) {
        document.documentElement.style.setProperty('--theme-color', themeColor)
    }
    if (subThemeColor) {
        document.documentElement.style.setProperty('--sub-theme-color', subThemeColor)
    }
}

onMounted(() => {
    getThemeColor()
    getCaptchaImage()
})

</script>
<style lang="less" scoped>
.content {
    /* 相对定位 */
    position: relative;
    /* 100%窗口高度 */
    height: 100vh;
    /* 渐变背景 */
    background: linear-gradient(200deg, var(--sub-theme-color), var(--theme-color));
    /* 溢出隐藏 */
    overflow: hidden;

    .container {
        height: 100%;
        /* 弹性布局 居中 */
        display: flex;
        justify-content: center;
        align-items: center;

        .form {
            z-index: 1;
            background-color: #fff;
            border-radius: 15px;
            /* 弹性布局 垂直排列 */
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 350px;
            height: 500px;
            /* 阴影 */
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);

            .tit {
                font-size: 26px;
                margin: 65px auto 34px auto;
            }

            .login {
                /* 弹性布局 垂直排列 */
                display: flex;
                flex-direction: column;
                align-items: center;

                input {
                    width: 280px;
                    height: 30px;
                    text-indent: 8px;
                    border: none;
                    border-bottom: 1px solid #ddd;
                    outline: none;
                    margin: 12px auto;
                }

                .code {
                    display: flex;
                    align-items: center;

                    input {
                        width: 180px
                    }

                    img {
                        width: 100px;
                        height: 20px;
                        cursor: pointer;

                    }
                }

            }

            .selectRole {
                width: 280px;
                margin-bottom: 12px;
            }

            button {
                width: 280px;
                height: 40px;
                margin: 35px auto 40px auto;
                border: none;
                background: linear-gradient(-200deg, var(--sub-theme-color), var(--theme-color));
                color: #fff;
                font-weight: bold;
                letter-spacing: 8px;
                border-radius: 10px;
                cursor: pointer;
                /* 动画过渡 */
                transition: 0.5s;
            }

            button:hover {
                background: linear-gradient(-200deg, var(--theme-color), var(--sub-theme-color));
                background-position-x: -280px;
            }

            span {
                font-size: 12px;
            }

            a {
                color: var(--theme-color);
                text-decoration: none;
            }

            .prompt {
                text-align: left;
                line-height: 12px;
                height: 12px;
                width: 280px;
                color: red;
            }
        }
    }
}

.list .item {
    position: absolute;
    border: 1px solid #fff;
    background-color: #fff;
    width: 30px;
    height: 30px;
    list-style: none;
    opacity: 0;
}

.square .item {
    top: 40vh;
    left: 60vw;
    /* 执行动画：动画名 时长 线性的 无限次播放 */
    animation: square 10s linear infinite;
}

.square .item:nth-child(2) {
    top: 80vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.square .item:nth-child(3) {
    top: 80vh;
    left: 85vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.square .item:nth-child(4) {
    top: 10vh;
    left: 70vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.square .item:nth-child(5) {
    top: 10vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

.circle .item {
    bottom: 0;
    left: 15vw;
    /* 执行动画 */
    animation: circle 10s linear infinite;
}

.circle .item:nth-child(2) {
    left: 35vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.circle .item:nth-child(3) {
    left: 55vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.circle .item:nth-child(4) {
    left: 75vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.circle .item:nth-child(5) {
    left: 90vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

/* 定义动画 */
@keyframes square {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
    }
}

@keyframes circle {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
        bottom: 0;
        border-radius: 0;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
        bottom: 90vh;
        border-radius: 50%;
    }
}

::v-deep(.el-select__wrapper) {
    border-radius: 10px;
}

::v-deep(.el-select__wrapper.is-focused) {
    box-shadow: 0 0 0 1px #dcdfe6;
}

::v-deep(.el-select__wrapper.is-hovering) {
    box-shadow: 0 0 0 1px #dcdfe6;
}
</style>